﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习八月份
{
    public class Solution3
    {
        public int LengthOfLongestSubstring(string s)
        {
            
            int len = s.Length;
            int res = 0;
            Dictionary<char, int> map = new Dictionary<char, int>();
            
            int left = 0;
            if (len == 1)
            {
                return 1;
            }

            while (left < len)
            {

                for (int i = left; i < len; i++)
                {
                    char cur = s[i];

                    if (!map.ContainsKey(cur))
                    {
                        map[cur] = i;

                    }
                    else
                    {

                        int pre = map[cur];

                        for (int j = left; j <= pre; j++)
                        {
                            //重复区间的从map里面除去
                            map.Remove(s[j]);
                        }
                        map[cur] = i;//更新位置
                        int curlen = i - left;
                        left = pre + 1;
                        res = Math.Max(res, curlen);
                        break;
                    }


                }


            }
            if(res == 0) { return len; }
            else
            {
                return res;
            }
          

        }
    }
}
